package com.tong.hao.ac.service.component.test;

import cn.buddha.jec.util.TongSpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author shihao
 * @description 策略环境类
 */
@Component
@ConditionalOnBean(value = TongSpringContextUtils.class)
@Slf4j
public class StrategyContext {

    /**
     * 存储接口 StrategyInterface 的实现类
     */
    private Map<String, StrategyInterface> typeMap = new HashMap<>();

    @PostConstruct
    public void init() {
        log.info("初始化接口StrategyInterface的实现类");
        Map<String, StrategyInterface> beansOfType =
                TongSpringContextUtils.getApplicationContext().getBeansOfType(StrategyInterface.class);
        beansOfType.values().forEach(strategyImpl -> {
            typeMap.put(strategyImpl.type(), strategyImpl);
        });
    }

    /**
     * @description // 根据不同的申请类型获取 StrategyInterface 接口的不同实现类
     **/
    public StrategyInterface getStrategyImpl(String type) {
        return typeMap.get(type);
    }

}
